//
//  VideoPlayerContainerView.swift
//  UGSwiftMagicbox
//
//  Created by MBA on 2020/10/3.
//  Copyright © 2020 MBA. All rights reserved.
//

import SwiftUI
import AVFoundation

struct VideoPlayerContainerView: View {
    
    // The progress through the video, as a percentage (from 0 to 1)
    @State private var videoPos: Double = 0
    // The duration of the video in seconds
    @State private var videoDuration: Double = 0
    // Whether we're currently interacting with the seek bar or doing a seek
    @State private var seeking = false
    @State private var status = AVPlayer.TimeControlStatus.paused
    
    private let player: AVPlayer
  
    init(url: URL) {
        player = AVPlayer(url: url)
    }
  
    var body: some View {
        VStack {
            
            VideoPlayerView(videoPos: $videoPos,
                            videoDuration: $videoDuration,
                            seeking: $seeking,
                            status:$status,
                            player: player)
            
            if status == .paused{
                Text(String(format: " paused"))
            }
            if status == .waitingToPlayAtSpecifiedRate{
                Text(String(format: " waitingToPlayAtSpecifiedRate"))
            }
            if status == .playing {
                Text(String(format: " playing"))
            }
            
            
            VideoPlayerControlsView(videoPos: $videoPos,
                                    videoDuration: $videoDuration,
                                    seeking: $seeking,
                                    player: player)
        }
        .onAppear(){
            
        }
        .onDisappear {
            self.player.replaceCurrentItem(with: nil)
        }
    }
}



struct VideoPlayerContainerView_Previews: PreviewProvider {
    static var previews: some View {
        Group {
            VideoPlayerContainerView(url: URL(string: "https://bitdash-a.akamaihd.net/content/sintel/hls/playlist.m3u8")!)
        
        }
    }
}
